package insert;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import conf.UserConfigure;

import util.MetaDataHandler;

public class InsertTableHbase {
	private static Configuration configuration = UserConfigure.configuration;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Configuration conf = null;
		conf = HBaseConfiguration.create();
		conf.addResource(new Path("./src/conf/user-connect.xml"));

		createTable2();
		
	}
	public static void createTable2()
	{
		String tableName = "tableHbase";
		ArrayList<String> columnFamilies = new ArrayList<String>();
		columnFamilies.add("cf1");
		columnFamilies.add("cf2");

		
		ArrayList<String> clms = new ArrayList<String>();
		clms.add("c1");
		clms.add("c2");
		clms.add("c3");
		clms.add("c4");
		clms.add("c5");
		
//		MetaDataHandler mdh = new MetaDataHandler();
		try {
			MetaDataHandler.createTable(tableName, columnFamilies);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		insertRandomData1(tableName, columnFamilies, clms);
		
		//now begin to insert data
	}
	public static void insertRandomData1(String table_name, ArrayList<String> cfs, ArrayList<String> clms)
	{
		HTable table = null;
		try {
			table = new HTable(configuration,table_name);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int recordNumber = 10000;
		Random rand = new Random();

		for(int i=0; i < recordNumber; i++)
		{
			int value = rand.nextInt(recordNumber+1);
			String rowkey = new Integer(i+1).toString();
			Put put = new Put(rowkey.getBytes());
			for(int j=0; j < cfs.size(); j++)
			{
				if(j==0)
				{
					put.add(cfs.get(j).getBytes(),clms.get(0).getBytes(),new Integer(value).toString().getBytes());
					put.add(cfs.get(j).getBytes(),clms.get(1).getBytes(),new Integer(value).toString().getBytes());
					put.add(cfs.get(j).getBytes(),clms.get(2).getBytes(),new Integer(value).toString().getBytes());
				}
				else
				{
					put.add(cfs.get(j).getBytes(),clms.get(3).getBytes(),new Integer(value).toString().getBytes());
					put.add(cfs.get(j).getBytes(),clms.get(4).getBytes(),new Integer(value).toString().getBytes());
				}
				
			}
			try {
				table.put(put);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(i%100==0)
				System.out.println("Insert " + i);
		}
		table.clearRegionCache();
		try {
			table.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
